<?php
/**
 * 主题模型
 * 
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0
 * @since 2011-06-03
 * @author chenyan
 */

class themeModel extends model
{
	
	/**************************************************************************
	 * 返回 值说明
	 * 1.返回的数组（theme）说明
	 * 	theme_id	int		主题ID
	 *  display 	string	显示名
	 *  theme    	string	标示
	 * 
	 * 
	 * 
	 * 2.若返回值为操作是否成功	1：成功      0或则负数：失败
	 **************************************************************************/
	
	
	
	/**
	 * 增加主题
	 * @param int $display	显示名
	 * @param int $theme	标示
	 * 
	 * @return int 新加入主题ID
	 */
	public function add($display,$theme){
		$data = array('display'=>$display,'theme'=>$theme);
		$theme_id = DB::insert('theme', $data);
		return $theme_id;
	}
	
	
	/**
	 * 删除主题
	 * @param int $theme_id 主题ID
	 * 
	 * @return  			1:成功  0:失败
	 */
	public function delete($theme_id){
		$condition = "theme_id=$theme_id";
		$row = DB::delete('theme',$condition);
		if($row>0) return 1;
		else return 0;
	}
	/*
	public function modify($theme_id,$display,$theme){
		$data = array('display'=>$display,'theme'=>$theme);
		$condition = "theme_id=$theme_id";
		$row = DB::update('theme',$data,$condition);
		if($row>0) return 1;
		else return 0;
	}
	*/
	/**
	 * 同步theme数据库与文件
	 * 
	 *  @return  			1:成功
	 */
	public function sync_theme(){
		//step 1: 遍历文件夹获取文件名及主题
		$dir = $_SERVER['DOCUMENT_ROOT'].'/image/theme/';
		$fileArray = array();
		if($handle = opendir($dir)){
			while(($file = readdir($handle)) !== false){
				if($file !="." && $file !=".."&& stristr($file,".css")){
					$curfile_dir = $dir.$file;
					$theme = substr($file,0,-4);
					
					$file_handle = fopen($curfile_dir, "r");
					while (!feof($file_handle)) {
					   $line = fgets($file_handle);
					   if(stristr($line,"@display")){
					   		$pos = strpos($line,"@display");
					   		$display = trim(substr($line,$pos+9));
					   		break;
					   }
					}
					fclose($file_handle);
					
					if($theme==''||$display=='') continue;
					$fileArray[]=array('theme'=>$theme,'display'=>$display);
				}
			}
		}
		//step 2: 同步数据库数据
		$delete_condition = '';
		foreach($fileArray as $fa){
			$theme = $fa['theme'];
			$display = $fa['display'];
			$row = DB::fetchOne("SELECT * FROM ".DB::table('theme')." where theme='$theme'");
			if(count($row) == 0){							//没有此theme，需要添加
				DB::insert('theme', array('display'=>$display,'theme'=>$theme));
			}elseif($row['display']!=$display){				//修改已存在thenme
				$data = array('display'=>$display);
				$condition = "theme=$theme";
				DB::update('theme',$data,$condition);
			}
			$delete_condition.="AND theme!='$theme' ";
		}
		//step3 :删除多余数据
		if($delete_condition!=''){
			$delete_condition = substr($delete_condition,4);
			DB::delete('theme',$delete_condition);
			//如果用户使用被删除theme，将用户的theme改为default；
			$data = array('theme' => 'default');
			$row = DB::update('user',$data,$delete_condition);
		}
		return 1;
	}
	
	/**
	 * 获取所有主题
	 * @return 二维数组
	 */
	public function get_themes(){
		return DB::fetchArray('SELECT * FROM '.DB::table('theme'));
	}
	
	/**
	 * 根据主题ID获取记录
	 * @param int $theme_id	主题ID
	 * 
	 * @return 一维数组
	 */
	public function get_theme_by_theme_id($theme_id){
		$row = DB::fetchOne("SELECT * FROM ".DB::table('theme')." where theme_id=$theme_id");
		if(count($row) == 0){
			return array();
		}
		return $row;
	}
	/**
	 * 根据标示获取theme
	 * @param string $theme	标示
	 * 
	 * @return 一维数组
	 */
	public function get_theme_by_theme($theme){
		$row = DB::fetchOne("SELECT * FROM ".DB::table('theme')." where theme='$theme'");
		if(count($row) == 0){
			return array();
		}
		return $row;
	}
}
?>